<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
	class name 	= main_model.php
	author		= Iswan Putera S.Kom
	version		= 2.0
	
*/

class Main_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
			
	}
	
	function table_name($table_name)
	{
		$this->table=$table_name;
	}
	
	function set_methods($method)
	{
		//method :replace,insert,update,delete
		$this->method=strtolower($method);
	}
		
	function get_content($data)
	{
		return $this->content=$data;
	}
	function set_data()
	{
		switch($this->method)
		{
			case 'replace':
				$this->db->replace($this->table,$this->content);
			break;
			case 'insert':
				$this->db->insert($this->table,$this->content); 
			break;
			case 'delete':
				$this->db->delete($this->table);
			break;	
			case 'select_detail':
				$this->db->select($this->sql);
				$data=$this->db->get($this->table);
				foreach($data->result() as $r)
				{
					$datax=array('userid'	=>$r->userid,
								 'username'	=>$r->username,
								 'nm_group' =>$r->nm_group,
								 'app_document'=>$r->app_document,
								 'useremail'=>$r->useremail,
								 'id_lokasi'=>$r->id_lokasi);
				}
			return $datax;
			break;
			case 'update':
				$this->db->update($this->table,$this->content);
			break;
			case 'select':
				$this->db->select($this->sql);
				$data=$this->db->get($this->table);
				return $data->result();
			break;
			case 'select_ordered':
				$this->db->select($this->sql);
				$this->db->order_by($this->ordby);
				$data=$this->db->get($this->table);
				return $data->result();
			break;
		}
	}
	function prepared_data($table,$data,$method='replace')
	{
		$this->main_model->table_name($table);
		$this->main_model->set_methods($method);
		$this->main_model->get_content($data);
		
	}
	function query($sql='*')
	{
		$this->sql=$sql;	
	}
	function orderby($ordb='')
	{
		$this->ordby=$ordb;	
	}
	
	//where function
	function where($key='')
	{
		$this->where=($key=='')?'':
		$this->db->where($key);
		
	}
	function where_or($key='')
	{
		$this->db->or_where($key);
	}
	function where_not($key='',$data='')
	{
		$this->db->where_not_in($key,$data);
	}
	function where_in($key='',$data='')
	{
		$this->db->where_in($key,$data);	
	}
	function where_like($key='',$data='',$posisi='both')
	{
		//$posisi otpion : % will be place on before,after or both variable data
		$this->db->like($key,$data,$posisi);
	}
	function where_or_like($key='',$data='',$posisi='both')
	{
		//$posisi otpion : % will be place on before,after or both variable data
		$this->db->or_like($key,$data,$posisi);
	}
	function group_by($field)
	{
		$this->db->group_by($field);	
	}
	function dropdwn_select($selected='')
	{
		$this->selected=$selected;	
	}
	function dropdown_data($id,$caption='')
	{
		$caption=($caption=='')?$id:$caption;
		$this->db->select();
		$this->where();
		$query=$this->db->get($this->table);
		//print_r($query->result());
		if($query){
			$data="<option value=''>&nbsp;</option>";
			foreach($query->result() as $r)
			{
				$select=($r->$id==$this->selected)?"selected='selected'":'';
				$data.="<option value='".$r->$id."' ".$select.">".$r->$caption."</option>";
			}
		}else{
			$data.="<option>&nbsp;</option>";
		}
		return $data;
	}
	
	function show_users($where,$data)
	{
		$this->db->where_not_in('nm_group','Superuser');
		$this->db->select();
		$data=$this->db->get($this->table);
		return $data->result();
	}
	
	function call_sp($sp_name,$field='',$fld2='')
	{
		$sql="call $sp_name($field)";
		$data=$this->db->query($sql);
		return $data->result();	
	}
	
	function rdb($tabel,$fld,$where='')
	{
		$f=explode('as ',$fld);
		$fl=(count($f)>1)?$f[1]:$fld;
		$data=array();$datax='';
		$sql="select $fld from $tabel $where";
		//echo $sql."\n";
		$data=$this->db->query($sql);
		foreach($data->result() as $r)
		{
			$datax=$r->$fl;
		}
		return $datax;
	}
	function hapus_data($table,$fieldkey,$data)
	{
		$this->main_model->table_name($table);
		$this->main_model->set_methods('delete');
		$this->main_model->where(array($fieldkey=>$data));
		$this->main_model->set_data();	
	}
	
	function update_data($table)
	{
		//$this->content and $this->where must do fisrt
		$this->main_model->table_name($table);
		$this->main_model->set_methods('update');
		$this->main_model->set_data();	
	}
	function generate_list($table='',$ordby='')
	{
			$this->main_model->table_name($table);
			$this->main_model->query();
			$this->main_model->orderby($ordby);
			$this->main_model->set_methods('select_ordered');
			$lst_usr=$this->main_model->set_data();
			return $lst_usr;
	}
	function countRecord($tabel,$where)
	{
		$this->db->where($where);
		return $this->db->count_all_results($tabel);	
	}
	
	function get_db($tabel='',$where='',$field='')
	{
		/**
		get default simpanan member
		*/
		$val=0;$lst_usr=array();
		$this->table_name($tabel);
		$this->query();
		$this->where($where);
		$this->set_methods('select');
			$lst_usr=$this->set_data();
			foreach($lst_usr as $rr)
			{
				$val=$rr->$field;
			}
		return $val;
	}
	

}